<template>
  <div class="loginbg">
    <el-row :gutter="20" style="padding-top: 10%;">
  <el-col :span="8" :offset="'2'-0" >
    <div class="block">
    <span style="padding-left: 30%; font-family: 'ZCOOLKuaiLe-Regular';font-size: 25px;">生活点滴不被遗落</span>
    <el-carousel height="400px">
      <el-carousel-item v-for="item in urls" :key="item">
        <el-image :src="item" ></el-image>
      </el-carousel-item>
    </el-carousel>
  </div>
  </el-col>
  <el-col :span="10" style="background-color: #ffff; height: 400px; padding-top: 5%;" :offset="'3'-0">
    
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <h1 class="login">登  录</h1>
      <el-form-item label="邮箱" prop="email">
    <el-input type="text" v-model="ruleForm.email" autocomplete="off" placeholder="请输入邮箱"  prop="email"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="请输入密码"  prop="pass" show-password></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')" :plain="true">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button><br>
    <el-link href="/register" :underline="false">还没账号? 注册一个</el-link>
    <br/>
    <el-link href="/updatepass" :underline="false">忘记密码</el-link>
  </el-form-item>

</el-form>
  </el-col>
</el-row>
  </div>
</template>

<script>
import axios from 'axios'
  export default {
    data() {  
      var checkEmail = (rule,value,cb) => {
        const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        if(regEmail.test(value)){
          //合法的邮箱
          return cb();
        }
        //邮箱不合法
        cb(new Error('请输入合法的邮箱！'));
	};
  var checkPass =  (rule,value,cb) => {
        const regPass = /^[a-zA-Z]\w{5,17}/;
        if(regPass.test(value)){
          //合法的邮箱
          return cb();
        }
        //邮箱不合法
        cb(new Error('请输入以字母开头，长度在6-18之间，只能包含字符、数字和下划线的密码'));
	};
      return {
        ruleForm: {
          pass: '',
          email:''
        },
        rules: {
          email: [
          {required: true, trigger: 'blur',validator: checkEmail},
          ],
          pass: [
          {required: true, trigger: 'blur',validator: checkPass,},
          ]
        },
        urls:[
          require("../assets/pc/lunbo1.jpg"),
          require("../assets/pc/lunbo2.jpg"),
          require("../assets/pc/lunbo3.jpg"),
          require("../assets/pc/lunbo4.png"),
        ],
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post(`/api/login?email=${this.ruleForm.email}&password=${this.ruleForm.pass}`).then(
              response=>{
                if(response.data.code===200){
                  this.$confirm('校验成功，是否登录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                 type: 'warning'
          }).then(() => {
            localStorage.setItem("nickname",response.data.data.nickName)
            localStorage.setItem("email",response.data.data.email)
            this.$router.push('/')
            
            
          this.$message({
            type: 'success',
            message: '登录成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消登录'
          });          
        });
                }else if(response.data.code===500){
                  this.$message.error("登录失败,"+response.data.message);
                }
              },
              error=>{
                this.$message.error("登录失败"+error.message);
              }
            )
          } else {
            this.$message.error("登录失败");
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
     
    }
  }
</script>

<style>
.loginbg{
  background:linear-gradient(rgb(239, 244, 255) 0%, rgba(255, 255, 255, 0) 100%);
}
.login{
  padding-bottom: 5px;
  padding-left: 50%;
  font-family: 'ZCOOLKuaiLe-Regular';
  font-size: 40px;
}
.el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }
</style>